﻿Chenxin.UI.allothouseManage = Chenxin.extend(Chenxin.UI.PageCommon, {
    autoInitForm: false,
    actionUrl: 'service/allotHouseMangager.ashx',
    createGrid: function() {
        return new Chenxin.UI.Pagination({
            actionUrl: this.actionUrl + '?op=getlist',
            domId: 'page',
            renderTo: 'allothousemanager_table'
        });
    },
    createForm: function() {
        return new Chenxin.UI.AllothouseForm({
            scope: this
        });
    },
    initEvent: function() {
        var _this = this;
        $('#allo_thouse').click(function() {
            _this.add();
            _this.form.reset();
            _this.form.setTitle('分配房源');
        });
        $('#keyword').defText({
            deftxt: '输入地址/编号/楼盘'
        });
        $('#attach_house').click(function() {
            _this.houseattach();
        });
    },
    houseattach: function() {
        var _this = this;
        if (!this.houseattachForm) {
            this.houseattachForm = new Chenxin.UI.HouseAttachForm({
                scope: _this,
                op:'attach'
            });
        }

        if (this.houseattachForm.show()) {
            this.houseattachForm.reset();
        }
    },
    createSearch: function() {
        return new Chenxin.UI.allothouseManageSearch({
            scope: this
        });
    },
    initComponent: function() {
        Chenxin.UI.allothouseManage.superclass.initComponent.call(this);
    }
});


/**
*查询面板
*/
Chenxin.UI.allothouseManageSearch = Chenxin.extend(Chenxin.UI.SearchCommon, {
    id: 'allothousemanager_search',
    initEvent: function() {
        var _this = this;
        $('#btn_search').click(function() {
            _this.scope.reloadGrid();
        });
},
createComboList: function() {
    var combl = new Chenxin.UI.StructComboList();
    var structs = combl.comboList;
    return structs;
},
initComponent: function() {
    Chenxin.UI.allothouseManageSearch.superclass.initComponent.call(this);
}
});

$(document).ready(function() {
    var allothouseMgr = new Chenxin.UI.allothouseManage();
});

/**
*房源归属
*/

Chenxin.UI.HouseAttachForm = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: 'service/allotHouseMangager.ashx',
    id: 'attachhouseWin',
    renderTo: 'jqm_attachhouse',
    jqmWidth: 700,
    getListOp: 'gethouselist',
    listAltMsg: '请选择房源！',
    getAlltCustomerOp: 'getallotusers',
    title: '房源归属',
    autoHide: false,
    reset: function() {
        Chenxin.UI.HouseAttachForm.superclass.reset.call(this);
        if (this.houseList1) {
            this.houseList1.grid.clear();
        }
    },
    initFormValidator: function() {
        var filter = '#' + this.id;
        $(filter + '_usercode').formValidator({
            validatorgroup: this.id,
            onshow: "请输入工号!",
            onfocus: "请输入工号!",
            oncorrect: " "
        }).inputValidator({ //非空验证
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "工号不能为空!"
            },
            onerror: "工号不能为空!"
        });
    },
    saveSucc: function() {
        //this.form.houseList.reload();
        this.grid.reload();
        //this.houseattachForm.houseList1.grid.clear();
        this.houseattachForm.houseList1.reloadGrid();
    },
    afterGetFormJson: function(json) {
        if (json.startDay) {
            delete json.startDay;
        }
        if (json.endDay) {
            delete json.endDay;
        }
        if (json.checkall) {
            delete json.checkall;
        }
        if (json.keyword) {
            delete json.keyword;
        }
        this.houseattachForm.additionalParam.houseIdList = json.allotck;
        this.houseattachForm.additionalParam.usercode = json.usercode;
        delete json.allotck;
        delete json.usercode;
    },
    createPage: function() {
        return new Chenxin.UI.AttachhousePage({
            actionUrl: this.actionUrl + '?op=' + this.getListOp
        });
    },
    getAlloOpType: function() {
        var _this = this;
        Chenxin.ajax({
            url: this.actionUrl + '?op=getallooptype',
            type: "get",
            dataType: "json",
            onSuccess: function(data, msg) {
                _this.findByName('OldPersonID').required = data.byuser;
            }
        });
    },
    createForm: function() {
        return Chenxin.FormCommon.AttachHouseMangagerForm(this.id);
    },
    customerFormVal: function(model) {
        if (!this.additionalParam.houseIdList) {
            alert(this.listAltMsg);
            return false;
        }
        return true;
    },
    createComboList: function() {

        var op_city = new Chenxin.UI.StructCombo({
            param: { op: 'getcity' },
            containerId: '#' + this.id + '_op_city',
            name: 'OldCityId',
            inModelType: 'all',
            txtModelName: 'OldCity',
            required: true,
            tbxWidth: 60,
            afterLoadItems: function() {
                if (this.items.length == 1) {
                    this.addPleaseSelected = false;
                    this.initValue = this.items[0].val;
                    op_company.param.fatherid = this.items[0].val;
                    op_company.reload();
                } else {
                    this.addPleaseSelected = true;
                }
            },
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    op_company.clearItems();
                } else {
                    op_company.param.fatherid = this.getValue();
                    op_company.reload();
                }
                op_departs.clearItems();
                op_emp.clearItems();
            }
        });

        //公司
        var op_company = new Chenxin.UI.StructCombo({
            autoLoad: false,
            param: { op: 'getcompany' },
            tbxWidth: 60,
            name: 'OldCompanyId',
            inModelType: 'all',
            txtModelName: 'OldCompany',
            containerId: '#' + this.id + '_op_company',
            afterLoadItems: function() {
                if (this.items.length == 1) {
                    this.addPleaseSelected = false;
                    this.initValue = this.items[0].val;
                    op_departs.param.fatherid = this.items[0].val;
                    op_departs.reload();
                } else {
                    this.addPleaseSelected = true;
                }
            },
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    op_departs.clearItems();
                } else {
                    op_departs.param.fatherid = this.getValue();
                    op_departs.reload();
                }
                op_emp.clearItems();
            }
        });
        //门店
        var op_departs = new Chenxin.UI.StructCombo({
            autoLoad: false,
            tbxWidth: 60,
            colWidth: 100,
            hasSearch: true,
            name: 'OldDepartID',
            inModelType: 'all',
            txtModelName: 'OldDepart',
            containerId: '#' + this.id + '_op_depart',
            param: { op: 'getallotdeparts' },
            hasPage: true,
            pageSize: 10,
            required: true,
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    op_emp.clearItems();
                } else {
                    op_emp.param.fatherid = this.getValue();
                    op_emp.reload();
                }
            }
        });
        //人
        var op_emp = new Chenxin.UI.StructCombo({
            autoLoad: false,
            tbxWidth: 60,
            colWidth: 120,
            name: 'OldPersonID',
            inModelType: 'all',
            txtModelName: 'OldPerson',
            containerId: '#' + this.id + '_op_emp',
            //required:true,
            param: { op: this.getAlltCustomerOp }
        });

        var aim_city = new Chenxin.UI.StructCombo({
            param: { op: 'getcity' },
            containerId: '#' + this.id + '_aim_city',
            name: 'NowCityId',
            inModelType: 'all',
            txtModelName: 'NowCity',
            required: true,
            tbxWidth: 60,
            afterLoadItems: function() {
                if (this.items.length == 1) {
                    this.addPleaseSelected = false;
                    this.initValue = this.items[0].val;
                    aim_company.param.fatherid = this.items[0].val;
                    aim_company.reload();
                } else {
                    this.addPleaseSelected = true;
                }
            },
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    aim_company.clearItems();
                } else {
                    aim_company.param.fatherid = this.getValue();
                    aim_company.reload();
                }
                aim_departs.clearItems();
            }
        });

        //公司
        var aim_company = new Chenxin.UI.StructCombo({
            autoLoad: false,
            param: { op: 'getcompany' },
            tbxWidth: 60,
            name: 'NowCompanyId',
            inModelType: 'all',
            txtModelName: 'NowCompany',
            containerId: '#' + this.id + '_aim_company',
            afterLoadItems: function() {
                if (this.items.length == 1) {
                    this.addPleaseSelected = false;
                    this.initValue = this.items[0].val;
                    aim_departs.param.fatherid = this.items[0].val;
                    aim_departs.reload();
                } else {
                    this.addPleaseSelected = true;
                }
            },
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    aim_departs.clearItems();
                } else {
                    aim_departs.param.fatherid = this.getValue();
                    aim_departs.reload();
                }
            }
        });
        //门店
        var aim_departs = new Chenxin.UI.StructCombo({
            autoLoad: false,
            tbxWidth: 60,
            colWidth: 100,
            hasSearch: true,
            name: 'NowDepartID',
            inModelType: 'all',
            txtModelName: 'NowDepart',
            containerId: '#' + this.id + '_aim_depart',
            param: { op: 'getallotdeparts' },
            hasPage: true,
            required: true,
            pageSize: 10
        });
        //人
        //        var aim_emp = new Chenxin.UI.StructCombo({
        //            autoLoad: false,
        //            tbxWidth: 60,
        //            colWidth: 120,
        //            name: 'NowPersonID',
        //            inModelType: 'all',
        //            txtModelName: 'NowPerson',
        //            containerId: '#' + this.id + '_aim_emp',
        //            required: true,
        //            param: { op: 'getemployee', usertype: 1 }
        //        });

        return [op_city, op_company, op_departs, op_emp, aim_city, aim_company, aim_departs];
    },
    initEvent: function() {
        var _this = this;


        $('#attachhouseWin_Save').live('click', function() {
            //_this.op = 'attach';
            _this.save();
        });
        $('#attachhouseWin_Cancle').live('click', function() {
            _this.hide();
        });
        $('#checkall').live('click', function() {
            _this.findByName('allotck').attr('checked', this.checked);
        });
    },
    initComponent: function() {
        Chenxin.UI.HouseAttachForm.superclass.initComponent.call(this);
        this.houseList1 = this.createPage();
        this.houseList1.search.comboList.push(this.findByName('OldCityId'), this.findByName('OldCompanyId'), this.findByName('OldDepartID'), this.findByName('OldPersonID'));
        this.getAlloOpType();
    }
});
Chenxin.UI.AttachhousePage = Chenxin.extend(Chenxin.UI.PageCommon, {
    autoInitForm: false,
    createGrid: function() {
        return new Chenxin.UI.Pagination({
            actionUrl: this.actionUrl,
            pageSize: 100,
            autoLoad: false,
            renderTo: 'house_list_1'
        });
    },
    createSearch: function() {
        return new Chenxin.UI.AttachhouseSearch({
            scope: this
        });
    },
    initComponent: function() {
        Chenxin.UI.AttachhousePage.superclass.initComponent.call(this);
        //this.getAlloOpType();
    }
});
Chenxin.UI.AttachhouseSearch = Chenxin.extend(Chenxin.UI.SearchCommon, {
    id: 'attachhouseWin_search',
    afterGetParamJson: function(model) {
        if (model.startDay && model.endDay) {
            if (Chenxin.Fn.compareDate(model.startDay, model.endDay)) {
                alert('开始日期不能大于结束日期！');
                this.autoReload = false;
                return;
            }
        }

        if (!model.OldPersonID) {
            alert('请选择员工！');
            this.autoReload = false;
            return;
        }
        this.autoReload = true;
    },
    initEvent: function() {
        var _this = this;
        $('#attachhouseWin_btn_search').live('click', function() {
            _this.scope.reloadGrid();
        });
    },
    initComponent: function() {
        Chenxin.UI.AttachhouseSearch.superclass.initComponent.call(this);
    }
});



/**
*房源归属表单
*/
Chenxin.FormCommon.AttachHouseMangagerForm = function(comboIdPrefix) {
    //return '<table class="tbl_f"><tbody><tr><th>分配方式：</th><td><input type="radio" name="allotStyle" value="10" checked="checked"/>快速  <input type="radio" name="allotStyle" value="20" />手动</td></tr><tr><td colspan="2"><table style="width:100%;" id="' + comboIdPrefix + '_search"><tr><th>被分配的对象：</th><td><div class="fl">城市</div><div style="z-index:911; margin-right: 5px;" id="' + comboIdPrefix + '_op_city" class="SelectWrapSingle"></div><div class="fl">公司</div><div style="z-index:910; margin-right: 5px;" id="' + comboIdPrefix + '_op_company" class="SelectWrapSingle"></div><div class="fl">部门</div><div style="z-index:909; margin-right: 5px;" id="' + comboIdPrefix + '_op_depart" class="SelectWrapSingle"></div><div class="fl">人员</div><div style="z-index:908; margin-right: 5px;" id="' + comboIdPrefix + '_op_emp" class="SelectWrapSingle"></div></td></tr><tr id="tr_date" style="display:none"><th>建档日期：</th><td><input style="width:115px;" name="startDay"  class="inputbox_n" type="text" onclick="WdatePicker({position:{left:this.getBoundingClientRect().left+1,top:this.getBoundingClientRect().top+22}});"  readonly="readonly" />  至  <input style="width:115px;" name="endDay"  class="inputbox_n" type="text" onclick="WdatePicker({position:{left:this.getBoundingClientRect().left+1,top:this.getBoundingClientRect().top+22}});"  readonly="readonly" /> <input id="' + comboIdPrefix + '_btn_search" type="button" value="" class="btn_s_0" /></td></tr></table></td></tr><tr id="tr_methed"><th>分配方法：</th><td><input type="radio" name="allotType" value="10" checked="checked"/>全部分配给人<input type="radio" name="allotType" value="20" />全部分配给店<input type="radio" name="allotType" value="30" />平均分配给店</td></tr><tr id="tr_list" style="display:none"><td colspan="2"><div id="house_list" style=\"height:300px;\"></div></td></tr><tr><th>接收的对象：</th><td><div class="fl">城市</div><div style="z-index:907; margin-right: 5px;" id="' + comboIdPrefix + '_aim_city" class="SelectWrapSingle"></div><div class="fl">公司</div><div style="z-index:906; margin-right: 5px;" id="' + comboIdPrefix + '_aim_company" class="SelectWrapSingle"></div><div class="fl">部门</div><div style="z-index:905; margin-right: 5px;" id="' + comboIdPrefix + '_aim_depart" class="SelectWrapSingle"></div><div class="fl">人员</div><div style="z-index:904; margin-right: 5px;" id="' + comboIdPrefix + '_aim_emp" class="SelectWrapSingle"></div></td></tr><tr><td colspan="2" align="right"><div style="float:right; margin-right:15px;"><a id="' + comboIdPrefix + '_Save" class="btn_n fl" href="#"><span>确定</span></a><a id="' + comboIdPrefix + '_Cancle"  class="btn_n fl" href="#"><span>取消</span></a></div></td></tr></tbody></table>';
return '<table class="tbl_f"><tbody><tr><th>分配的对象：</th><td><div class="fl">城市</div><div style="z-index:911; margin-right: 5px;" id="' + comboIdPrefix + '_op_city" class="SelectWrapSingle"></div><div class="fl">公司</div><div style="z-index:910; margin-right: 5px;" id="' + comboIdPrefix + '_op_company" class="SelectWrapSingle"></div><div class="fl">部门</div><div style="z-index:909; margin-right: 5px;" id="' + comboIdPrefix + '_op_depart" class="SelectWrapSingle"></div><div class="fl">人员</div><div style="z-index:908; margin-right: 5px;" id="' + comboIdPrefix + '_op_emp" class="SelectWrapSingle"></div></td></tr><tr><td colspan="2"><table style="width:100%;" id="' + comboIdPrefix + '_search"><tr id="tr_date" ><th>建档日期：</th><td><input style="width:115px;" name="startDay"  class="inputbox_n" type="text" onclick="WdatePicker({position:{left:this.getBoundingClientRect().left+1,top:this.getBoundingClientRect().top+22}});"  readonly="readonly" />  至  <input style="width:115px;" name="endDay"  class="inputbox_n" type="text" onclick="WdatePicker({position:{left:this.getBoundingClientRect().left+1,top:this.getBoundingClientRect().top+22}});"  readonly="readonly" />&nbsp;&nbsp;&nbsp;&nbsp;关键字：<input id="keyword" name="keyword" class="inputbox_n" type="text" style="width:150px;" /> <input id="' + comboIdPrefix + '_btn_search" type="button" value="" class="btn_s_0" /></td></tr></table></td></tr><tr id="tr_list"><td colspan="2"><div id="house_list_1" style=\"height:200px;\"></div></td></tr><tr><th>接收的对象：</th><td><div class="fl">城市</div><div style="z-index:907; margin-right: 5px;" id="' + comboIdPrefix + '_aim_city" class="SelectWrapSingle"></div><div class="fl">公司</div><div style="z-index:906; margin-right: 5px;" id="' + comboIdPrefix + '_aim_company" class="SelectWrapSingle"></div><div class="fl">部门</div><div style="z-index:905; margin-right: 5px;" id="' + comboIdPrefix + '_aim_depart" class="SelectWrapSingle"></div><div class="fl">人员</div><input id="' + comboIdPrefix + '_usercode" name="usercode" class="inputbox_n" type="text" style="width:90px;" /><div id="'+comboIdPrefix+'_usercodeTip" style="float: right; margin-left: 5px; width: 90px; height: 26px; line-height: 26px; text-align: left;" class="fl"></div></td></tr><tr><td colspan="2" align="right"><div style="float:right; margin-right:15px;"><a id="' + comboIdPrefix + '_Save" class="btn_n fl" href="#"><span>确定</span></a><a id="' + comboIdPrefix + '_Cancle"  class="btn_n fl" href="#"><span>取消</span></a></div></td></tr></tbody></table>';
};

